# -*- coding: utf-8 -*-
# @Time : 2023/12/19 17:15
# @Author : debbyszhang
# @File : c1.py
# @desc :
import importlib
import inspect
import os

import xlwt as xlwt

PROJECT_ROOT = '/Users/debby/PycharmProjects/testcase_v233/chainmaker-autotest'
# from settings import PROJECT_ROOT

testcases = os.path.join(PROJECT_ROOT, 'testcases', 'function', 'permission', 'pk_tbft', 'default_permission')


def get_classes(file_path):
    with open(file_path, 'r') as file:
        lines = file.readlines()

    for line in lines:
        if line.startswith('class'):
            class_name = line.split(' ')[1].split('(')[0]
            return class_name.strip()

    return None


print(testcases)
print("PROJECT_ROOT===>" + str(PROJECT_ROOT))


def collect(testcases):
    cases = []
    for root, dirs, files in os.walk(testcases):
        for file in files:
            case = {}
            if file.endswith('.py') and not file.startswith('__'):
                file_path = os.path.join(root, file)
                script_path = os.path.join(root, file).replace(str(PROJECT_ROOT), '')
                file_module_path = script_path[:-3].replace('/', '.').lstrip('.')
                cls_list = get_classes(file_path)
                try:
                    module = importlib.import_module(file_module_path)
                    class_obj = getattr(module, cls_list)
                    class_doc = inspect.getdoc(class_obj)
                    method_doc = inspect.getdoc(class_obj.run_test)
                    split_result = method_doc.split("预期结果：")
                    test_case_steps = split_result[0].strip()
                    expected_result = split_result[1].strip()
                    case['用例名称'] = class_doc
                    case['需求ID'] = '1014001'
                    case['用例步骤'] = test_case_steps
                    case['预期结果'] = expected_result
                    case['用例类型'] = '功能测试'
                    case['用例状态'] = '正常'
                    case['用例等级'] = 'P1'
                    case['创建人'] = '张凤双'
                    case['是否可自动化'] = 'Python实现'
                    case['迭代版本'] = 'v2.3.2'
                    case['脚本路径'] = script_path
                    cases.append(case)
                    # print(class_obj.owner)
                    # print(f'用例标题：{class_doc}\n\n操作步骤：\n{test_case_steps}\n\n预期结果：\n{expected_result}\n\n脚本路径：{script_path}\n\n')
                except Exception as e:
                    print(e, script_path)
    return cases


def write_to_excel(file_name, data):
    workbook = xlwt.Workbook()
    sheet = workbook.add_sheet('Sheet1')

    # 写入表头
    sheet.write(0, 0, '用例名称')
    sheet.write(0, 1, '需求ID')
    sheet.write(0, 2, '用例步骤')
    sheet.write(0, 3, '预期结果')
    sheet.write(0, 4, '用例类型')
    sheet.write(0, 5, '用例状态')
    sheet.write(0, 6, '用例等级')
    sheet.write(0, 7, '创建人')
    sheet.write(0, 8, '是否可自动化')
    sheet.write(0, 9, '迭代版本')
    sheet.write(0, 10, '脚本路径')

    # 写入内容
    for row, item in enumerate(data, start=1):
        sheet.write(row, 0, item['用例名称'])
        sheet.write(row, 1, item['需求ID'])
        sheet.write(row, 2, item['用例步骤'])
        sheet.write(row, 3, item['预期结果'])
        sheet.write(row, 4, item['用例类型'])
        sheet.write(row, 5, item['用例状态'])
        sheet.write(row, 6, item['用例等级'])
        sheet.write(row, 7, item['创建人'])
        sheet.write(row, 8, item['是否可自动化'])
        sheet.write(row, 9, item['迭代版本'])
        sheet.write(row, 10, item['脚本路径'])

    # 保存Excel文件
    workbook.save(file_name)


if __name__ == '__main__':
    cases = collect(testcases)
    write_to_excel("test_cases.xls", cases)
